package com.turing.post.common.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.common.util.SysOpMenuGroup;
import com.turing.post.common.util.SysOpMenuGroupDao;

/**
 * @author zhang xiao pan
 * @创建时间：Dec 15, 2012
 */
@Stateless(mappedName = "common.impl.SysOpMenuGroupDaoImpl")
@Remote
@Local
public class SysOpMenuGroupDaoImpl implements SysOpMenuGroupDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	public List<SysOpMenuGroup> getSysOpMenuGroupListByRoleId(String roleIds) {
		String sql = "select ss.menugrpid,ss.menugrpname,ss.menugrpicon from sysopmenugroup ss,"
				+ "(select distinct(st.supermenuid) supermenuid from sysopmenu st where st.menuid in("
				+ "select distinct(ty.supermenuid) from sysopsap ts,sysopacl t,sysopmenu ty where ts.sapid=t.sapid and ts.menuid=ty.menuid "
				+ "and t.roleid in ("+roleIds+"))) sb where ss.menugrpid=sb.supermenuid order by ss.menugrpseqno";
		Query query = _em.createNativeQuery(sql);
		List<Object[]> result = query.getResultList();
		List<SysOpMenuGroup> resultList = new ArrayList<SysOpMenuGroup>();
		SysOpMenuGroup somg = null;
		for (int i = 0; i < result.size(); i++) {
			Object[] record = result.get(i);
			somg = new SysOpMenuGroup();
			somg.setMenuGrpId(((BigDecimal) record[0]).toBigInteger());
			somg.setMenuGrpName((String) record[1]);
			somg.setMenuGrpIcon((String) record[2]);
			resultList.add(somg);
		}
		return resultList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysOpMenuGroup> getSysOpMenuGroupList() {
		return _em.createNativeQuery(
				"select * from SysOpMenuGroup c where c.menugrpid<>1 order by menuGrpSeqNo",
				SysOpMenuGroup.class).getResultList();
	}
}
